#ifndef __EEPROM_H__
#define __EEPROM_H__

#include <REGX52.H>

sbit SDA=P2^0;
sbit SCL=P2^1;


void delay5us()
{
	
}

void Start()
{
	SDA=1;
	delay5us();
	SCL=1;
	delay5us();
	SDA=0;
	delay5us();
}

void Stop()
{
	SDA=0;
	delay5us();
	SCL=1;
	delay5us();
	SDA=1;
	delay5us();
}

void Respons()
{
	unsigned char i;
	SCL=1;
	delay5us();
	while((SDA==1)&&(i<255))
		i++;
	SCL=0;
	delay5us();
}

void Init()
{
	SDA=1;
	delay5us();
	SCL=1;
	delay5us();
}

void WrByte(unsigned char Dat)
{
	unsigned char i,t;
	t=Dat;
	for(i=0;i<8;i++)
	{
		t=t<<1;
		SCL=0;
		delay5us();
		SDA=CY;
		delay5us();
		SCL=1;
		delay5us();
	}
	SCL=0;
	delay5us();
	SDA=1;
	delay5us();
}

unsigned char RdByte()
{
	unsigned char i,k;
	SCL=0;
	delay5us();
	SDA=1;
	delay5us();
	for(i=0;i<8;i++)
	{
		SCL=1;
		delay5us();
		k=(k<<1)|SDA;
		SCL=0;
		delay5us();
	}
	return k;
}

void ROM_Add(unsigned char address,unsigned char Dat)
{
	Start();
	WrByte(0xa0);
	Respons();
	WrByte(address);
	Respons();
	WrByte(Dat);
	Respons();
	Stop();
}

unsigned char ROM_Use(unsigned char address)
{
	unsigned char Dat;
	Start();
	WrByte(0xa0);
	Respons();
	WrByte(address);
	Respons();
	Start();
	WrByte(0xa1);
	Respons();
	Dat=RdByte();
	Stop();
	return Dat;
}

#endif
